{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.neighbors import NearestNeighbors\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# X为6个点的数据\n",
    "X = np.array([[3, 2], [-2, -1], [-1, -1], [1, 1], [2, 1], [3, 2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 以 X[i] 为参考点，找(n_neighbors - 1)个与之最近的点\n",
    "nbrs = NearestNeighbors(n_neighbors = 3 , algorithm = 'ball_tree').fit(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 返回值 indices 第0列元素为参考点的索引，后面是(n_neighbors - 1)个与之最近的点的索引\n",
    "# 返回值 distances 第0列元素为与自身的距离，后面是(n_neighbors - 1)个与之最近的点与参考点的距离\n",
    "distances, indices = nbrs.kneighbors(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 5, 4],\n",
       "       [1, 2, 3],\n",
       "       [2, 1, 3],\n",
       "       [3, 4, 0],\n",
       "       [4, 3, 0],\n",
       "       [0, 5, 4]], dtype=int64)"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.        ,  0.        ,  1.41421356],\n",
       "       [ 0.        ,  1.        ,  3.60555128],\n",
       "       [ 0.        ,  1.        ,  2.82842712],\n",
       "       [ 0.        ,  1.        ,  2.23606798],\n",
       "       [ 0.        ,  1.        ,  1.41421356],\n",
       "       [ 0.        ,  0.        ,  1.41421356]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "distances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.  0.  0.  0.  1.  1.]\n",
      " [ 0.  1.  1.  1.  0.  0.]\n",
      " [ 0.  1.  1.  1.  0.  0.]\n",
      " [ 1.  0.  0.  1.  1.  0.]\n",
      " [ 1.  0.  0.  1.  1.  0.]\n",
      " [ 1.  0.  0.  0.  1.  1.]]\n"
     ]
    }
   ],
   "source": [
    "#输出的是求解n个最近邻点后的矩阵图，1表示是最近点，0表示不是最近点\n",
    "print (nbrs.kneighbors_graph(X).toarray())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
